package com.example.blog.service;

import com.example.blog.mapper.BlogMapper;
import com.example.blog.mapper.UserInfoMapper;
import com.example.blog.model.BlogInfo;
import com.example.blog.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    public UserInfo selectByName(String userName) {
        return userInfoMapper.selectByName(userName);
    }

    public UserInfo selectById(Integer userId) {
        return userInfoMapper.selectById(userId);
    }

    public UserInfo getAuthorInfo(Integer blogId) {
        //1、根据博客id，返回作者id
        BlogInfo blogInfo = blogMapper.selectById(blogId);
        if(blogInfo == null && blogInfo.getUserId()<=0){
            log.error("获取图书不存在或者图书作者信息不合法，blog{}",blogId);
            return null;
        }
        Integer id = blogInfo.getId();
        //2、根据作者id，获取作者信息
        UserInfo userInfo = userInfoMapper.selectById(id);
        return  userInfo;
    }
}
